package com.itheima.mp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{
    @Autowired
    private UserMapper userMapper;



    public void updateMoney(Long id , Long amount){
        User user = getById(id);
        if(user == null || user.getStatus() == 2){
            //return ;
            throw new RuntimeException("状态异常");
        }
        //查完状态进行操作
        if(user.getBalance()<amount){
            throw new RuntimeException("余额不足");
        }
        baseMapper.deductBalance(id,amount);
    }

    @Override
    public List<User> queryUsers(String name, Integer status, Integer minBalance, Integer maxBalance) {
        return lambdaQuery().like(name != null , User::getUsername,name)
                     .eq(status != null , User::getStatus,status)
                     .gt(minBalance != null , User::getBalance,minBalance)
                    .le(maxBalance != null , User::getBalance,maxBalance)
                     .list();

    }
}
